package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.VipDTO;
import com.woniu.pojo.User;
import com.woniu.pojo.Vip;
import com.woniu.service.IUserService;
import com.woniu.service.IVipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * vip表 前端控制器
 * </p>
 *
 * @author xiaolin
 * @since 2023-04-13
 */
@Api(tags = "Vip管理")
@RestController
@RequestMapping("/vip")
public class VipController extends BaseController {

    @Autowired
    private IVipService vipService;

    @Autowired
    private IUserService userService;

    /*查询Vip表并分页*/
    @PostMapping("/searchPage/{currentPage}/{pageSize}")
    @ApiOperation("分页查询")
    public Result searchPage(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody VipDTO vipDTO) {
        PageData pageData = vipService.searchPage(currentPage, pageSize, vipDTO);
        return toDataResult(pageData);
    }

    /*新增Vip*/
    @PostMapping("/insert")
    @ApiOperation("新增vip")
    public Result increase(@RequestBody Vip vip) {

        //若该vip名字不是user表中的名字 说明此人不存在 更不应该存在在vip表中  返回false
        List<User> userList = userService.list();
        Boolean flag = false;
        for (User one : userList) {
            if (one.getName().equals(vip.getName())) {
                flag = true;
                break;
            }

        }

        //若该vip名字已经存在于vip表中 返回false
        List<Vip> vipList = vipService.list();
        for (Vip one : vipList) {
            if (one.getName().equals(vip.getName())) {
                flag = false;
                break;
            }
        }

        //若设置到期时间小于当前时间 返回false
        int compareTo = vip.getExpirationTime().compareTo(new Date());
        if (compareTo <= 0)
            flag = false;

        vip.setOpeningTime(new Date());

        if (flag){
            QueryWrapper<User> queryWrapperUser = new QueryWrapper<>();
            queryWrapperUser.eq("name",vip.getName());
            User one = userService.getOne(queryWrapperUser);
            one.setVip(1);
            userService.updateById(one);
            return toResult(vipService.save(vip));
        }
        else
            return toResult(flag);
    }

    /*修改Vip*/
    @PostMapping("/update")
    @ApiOperation("修改vip")
    public Result revise(@RequestBody Vip vip) {

        Vip one = vipService.getById(vip.getId());
        int compareTo = vip.getExpirationTime().compareTo(one.getExpirationTime());

        //若修改后的vip名字不等于修改前的名字 返回false
        if (!one.getName().equals(vip.getName())) {
            return toResult(false);
        } else if (compareTo <= 0) {//若修改后的到期时间小于当前时间 返回false
            return toResult(false);
        }
        vip.setUpdateTime(new Date());

        return toResult(vipService.updateById(vip));
    }

    /*单个删除Vip*/
    @PostMapping("/deleteById/{id}")
    @ApiOperation("删除Vip")
    public Result decrease(@PathVariable Integer id) {

        //得到被删除的vip用户
        Vip oneVip = vipService.getById(id);

        //通过vip用户信息得到vip用户名 通过vip用户名查询用户表中该用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",oneVip.getName());
        User oneUser = userService.getOne(queryWrapper);
        oneUser.setVip(0);
        userService.updateById(oneUser);

        //删除vip表中的vip用户信息
        Boolean flag = vipService.removeById(id);
        return toResult(flag);

    }

    /*批量删除Vip*/
    @PostMapping("/deletePart")
    @ApiOperation("批量删除Vip")
    public Result deletePart(@RequestBody List<Integer> ids) {

        //得到被删除的vip集合
        List<Vip> vipList = vipService.listByIds(ids);

        //遍历被删除的vip集合得到每一个被删除vip的用户信息
        for (Vip vip:vipList){
            //通过vip用户信息得到vip用户名 通过vip用户名查询用户表中该用户信息
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name",vip.getName());
            User oneUser = userService.getOne(queryWrapper);
            oneUser.setVip(0);
            userService.updateById(oneUser);
        }

        boolean flag = vipService.removeBatchByIds(ids);
        return toResult(flag);
    }

    //修改数据所需的按id查询
    @GetMapping("/searchById/{id}")
    @ApiOperation("按id查询")
    public Result searchById(@PathVariable Integer id) {
        return toDataResult(vipService.getById(id));
    }

    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void exportExcel(@RequestBody(required = false) VipDTO vipDTO,
                            HttpServletResponse response) throws IOException {
        PageData pageData = vipService.searchPage(1, 1000, vipDTO);
        List<Vip> vipList = (List<Vip>) pageData.getData();
        EasyExcel.write(response.getOutputStream(), Vip.class).sheet().doWrite(vipList);
    }
}
